# 读取网络json文件
# import requests

# url = 'http://124.70.135.97:9002/temp/newgrid/%E6%8A%A4%E6%A0%91%E8%AE%BE%E6%96%BD.json'
# # url = 'http://124.70.135.97:9002/temp/newgrid/L3LabelPoint.json'
# response = requests.get(url)
# # print(response.json())

# features = response.json()['features']
# # print(features)
# num = len(features)
# print(num)


# 读取本地json文件
import json
import os

# 获取当前脚本文件的绝对路径
dir_path = os.path.dirname(os.path.realpath(__file__))

# 源文件和目标文件路径
source_file = os.path.join(dir_path, 'json', '护树设施.json')
target_file = os.path.join(dir_path, 'json', '护树设施_compressed.json')

try:
    # 读取源JSON文件
    with open(source_file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # 写入压缩后的JSON文件
    with open(target_file, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, separators=(',', ':'))
    
    # 获取文件大小进行对比
    source_size = os.path.getsize(source_file) / 1024  # 转换为KB
    target_size = os.path.getsize(target_file) / 1024  # 转换为KB
    
    print(f'原始文件大小: {source_size:.2f} KB')
    print(f'压缩后文件大小: {target_size:.2f} KB')
    print(f'压缩率: {((source_size - target_size) / source_size * 100):.2f}%')

except FileNotFoundError:
    print("找不到指定的文件")
except json.JSONDecodeError:
    print("JSON文件格式错误")
except Exception as e:
    print(f"发生错误: {str(e)}")


